﻿#ifndef CUNCHULIANBIAO_H
#define CUNCHULIANBIAO_H
#pragma execution_character_set("utf-8") //解决中文乱码

#include <iostream>
#include <QObject>
#include <QMutex>




class cunchulianbiao  : public QObject
{
    Q_OBJECT
public:
    explicit cunchulianbiao(QObject *parent = nullptr);
private:
    struct Node {
        unsigned long long       dizhiaddress;
        unsigned long long       value;
        unsigned long long       oldvalue;
        Node*                    prev;
        Node*                    next;
    };

    struct sendercanshu {
        unsigned long long jiancha2=1;
        unsigned long long jiancha;
        unsigned int processID;
        int xuhao;
        int guize;
        cunchulianbiao *lianbiao;
    };
    Node* head=nullptr;//头指针
    Node* tail=nullptr;//尾指针
    sendercanshu * canshu;

public:
    //cunchulianbiao() : head(nullptr), tail(nullptr) {}

    uint lianbiaogeshu = 0;
    bool shoucisaomiao = 0;

    ~cunchulianbiao() {
        while (head) {
            Node* next = head->next;
            delete head;
            head = next;
        }
    }

    Node* returnhead()
    {
        return head;
    }

    Node* returntail()
    {  //std::cout<<head->next<<std::endl;
        return tail;
    }

    void append( unsigned long long dizhiaddress,void * value) {
        if (head) {
            Node* newNode = new Node();
            newNode->dizhiaddress = dizhiaddress;
            switch (canshu->xuhao) {
            case 0:
                newNode->value = *(int *)value;
                break;
            case 1:
                newNode->value = *(char *)value;
                break;
            case 2:
                newNode->value = *(wchar_t *)value;
                break;
            case 3:
                newNode->value = *(unsigned long long *)value;
                break;
            case 4:
                float * a;
                a=(float *)&newNode->value;
                *a = *(float *)value;
                //std::cout<<*a<<std::endl;
                break;
            case 5:
                double * b;
                b=(double *)&newNode->value;
                *b = *(double *)value;
                //std::cout<<*b<<std::endl;
                break;
            case 6:
                //字符串
                break;
            }
            newNode->prev = tail;
            newNode->next = nullptr;
            tail->next = newNode;
            tail = newNode;
        } else {
            Node* newNode = new Node();
            newNode->dizhiaddress = dizhiaddress;
            switch (canshu->xuhao) {
            case 0:
                newNode->value = *(int *)value;
                break;
            case 1:
                newNode->value = *(char *)value;
                break;
            case 2:
                newNode->value = *(wchar_t *)value;
                break;
            case 3:
                newNode->value = *(unsigned long long *)value;
                break;
            case 4:
                float * a;
                a=(float *)&newNode->value;
                *a = *(float *)value;
                break;
            case 5:
                double * b;
                b=(double *)&newNode->value;
                *b = *(double *)value;
                break;
            case 6:
                //字符串
                break;
            }
            newNode->prev = tail;
            newNode->next = nullptr;
            head = tail = newNode;
        }
        lianbiaogeshu++;
        //std::cout<<lianbiaogeshu<<std::endl;

    }


    void shanchu(void  * nodel){

        Node* node = (Node*)nodel;

        if (node == head) {
            if(node == tail){
                tail = nullptr;
            }
            head = head->next;
            if (head != nullptr) {
                head->prev = nullptr;
            }
        } else {
            node->prev->next = node->next;
            if (node->next != nullptr) {
                node->next->prev = node->prev;
            }
        }
        delete node;
        lianbiaogeshu--;
     }
    void qingkong()
    {
        while (head!=nullptr) {
            Node* next = head->next;
            delete head;
            head = next;
            lianbiaogeshu=0;
        }
    }

public slots:
    void recveshu(void * dizhi){
        canshu = (sendercanshu *)dizhi;
    }
};

#endif // CUNCHULIANBIAO_H
